package com.xm.zcf.action.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xm.zcf.domain.adminuser.Adminuser;
import com.xm.zcf.domain.user.User;
import com.xm.zcf.service.admin.AdminService;
import com.xm.zcf.service.adminUser.AdminUserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/6.
 */
@Controller
@Scope("prototype")
public class AdminAction extends ActionSupport implements ModelDriven<User>{
    private User model =new User();
    private Integer uid;



    @Resource
    private AdminService adminService;
    @Resource
    private AdminUserService adminUserService;

    public String loginUI(){
        return "loginUI";
    }
    public String login(){
        User existUser=adminService.findUser(model.getUsername(),model.getPassword());
        if(existUser!=null){
            ActionContext.getContext().getSession().put("user",existUser);
            return "loginSuccess";
        }
        this.addActionMessage("用户名或者密码错误");
        return "loginFail";
    }
    public String findAllUser(){
        List<User>userList=adminUserService.findAllUser();
        ActionContext.getContext().put("userlist",userList);
        return "userList";
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String deleteUser(){
        Adminuser adminuser=adminUserService.findById(uid);
        adminUserService.deleteUser(adminuser);
        return "toUserList";
    }
    @Override
    public User getModel() {
        return model;
    }
}
